




import sys, os

import cv2, time, gc, string, queue, argparse, threading, random
import numpy as np




    
def Pass(queueIn, nStopIn=1):
    while nStopIn:
        info = queueIn.get()
        if info is None:
            nStopIn-=1




def videoLoaderLimit(queueOut, cap, startIndex, endIndex):
    print('start videoLoader'+str(startIndex))
    totalframe = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    frameindex = 0
    if startIndex>0 and startIndex<totalframe:
        cap.set(cv2.CAP_PROP_POS_FRAMES, startIndex)
        frameindex=startIndex
    if endIndex>frameindex and endIndex<totalframe:
        ret,frame = cap.read()
        while ret:
            queueOut.put([[frameindex, '2020-20-20 20:20:20'], frame])
            # if frameindex==2070:
                # cv2.imwrite('frame'+str(frameindex)+'.png', frame)
            frameindex += 1
            if frameindex>=endIndex:
                break
            ret,frame = cap.read()
            # print('cap.read() reading frameindex:{}/{}, {}fps'.format(frameindex, endIndex, fps))
    else:
        ret,frame = cap.read()
        while ret:
            queueOut.put([[frameindex, '2020-20-20 20:20:20'], frame])
            frameindex += 1
            ret,frame = cap.read()
            # print('cap.read() reading frameindex:{}/{}, {}fps'.format(frameindex, totalframe, fps))
            
    queueOut.put(None)
    print('end videoLoader'+str(frameindex))

    
def jumpt(path, n=4):

    t0=time.time()    
    capl = [ cv2.VideoCapture(path) for i in range(n) ]
    qtmp = [ queue.Queue(2) for i in range(n) ]
    totalframe = int(capl[0].get(cv2.CAP_PROP_FRAME_COUNT))
    step=int(totalframe/n)+1
    thl = [ threading.Thread(target=videoLoaderLimit, args=(qtmp[i], capl[i], i*step, (i+1)*step)) for i in range(n-1) ]
    thl.extend( [ threading.Thread(target=Pass, args=(qtmp[i], 1)) for i in range(n) ] )
    for thi in thl:
        thi.start()
    videoLoaderLimit(qtmp[n-1], capl[n-1], (n-1)*step, totalframe)
    for thi in thl:
        thi.join()
    et=time.time()-t0
    print('time='+str(et)+'\tfps='+str(totalframe/et))


def Write(path, startIndex, endIndex, imgExt='.jpg'):
    cap=cv2.VideoCapture(path)
    qtmp = queue.Queue(2)
    thl = threading.Thread(target=videoLoaderLimit, args=(qtmp, cap, startIndex, endIndex))
    thl.start()
    name, ext=os.path.splitext(path)
    nStopIn=1
    while nStopIn:
        info = qtmp.get()
        if info is None:
            nStopIn-=1
        else:
            cv2.imwrite(name+'_frame'+str(info[0][0])+imgExt, info[1])
    thl.join()


if __name__=='__main__':
    path="/home/u/20200702/HD_video1.mp4"
    path="test.mp4"

    # Write(path, 0, 100)
    # jumpt(path, 4)

    t1=cv2.VideoCapture('../t1.mp4').get(cv2.CAP_PROP_FRAME_COUNT)
    t2=cv2.VideoCapture('../t2.mp4').get(cv2.CAP_PROP_FRAME_COUNT)
    t3=cv2.VideoCapture('../test.mp4').get(cv2.CAP_PROP_FRAME_COUNT)
    
    print(t1)
    print(t2)
    print(t3)
    print(t1+t2-t3)


            
